package cn.edu.scau.cmi.yangqing.hibernate.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


/**
 * 该工具类提供了一个属性：SessionFactory sessionFactory 
 * 并创建了sessionFactory 将它设置成static 这样其他程序就可以直接通过此工具类引用
 * 提供了二个方法:
 * 1：通过线程创建Session-->currentSession()
 * 2：关闭Session-->closeSession()
 * 需要在主类中手动关闭sessionFactory
 */
public class HibernateUtil{
	
	private static  SessionFactory sessionFactory = null;
	private static  SessionFactory sessionFactoryStu = null;
	//创建sessionFactory
	static{
		Configuration configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		
		Configuration configurationStu = new Configuration().configure();
		sessionFactoryStu = configurationStu.buildSessionFactory();
	}
	

	public static Session getSession() {
		return sessionFactory.openSession();
	}
	
	public static Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}
	
	public static Session getSessionStu() {
		return sessionFactoryStu.openSession();
	}
	public static Session getCurrentSessionStu() {
		return sessionFactoryStu.getCurrentSession();
	}
}





